﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TLSVA06
    {

        #region ==VKOLL===========
        public static readonly DependencyProperty VKOLLProperty = DependencyProperty.Register("VKOLL",
            typeof(string), typeof(TLSVA06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVKOLLChanged));
        public string VKOLL
        {
            get
            {
                return (string)GetValue(VKOLLProperty);
            }
            set
            {
                SetValue(VKOLLProperty, value);
                OnKOLLChg();
            }
        }
        private static void OnVKOLLChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TLSVA06;
            if (src != null)
            {
                src.VKOLL = (string)e.NewValue;
            }
        }
        #endregion
        

        #region ==PDART===========
        public static readonly DependencyProperty PDARTProperty = DependencyProperty.Register("PDART",
            typeof(string), typeof(TLSVA06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPDARTChanged));
        public string PDART
        {
            get
            {
                return (string)GetValue(PDARTProperty);
            }
            set
            {
                SetValue(PDARTProperty, value);
            }
        }
        private static void OnPDARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TLSVA06;
            if (src != null)
            {
                src.PDART = (string)e.NewValue;
            }
        }
        #endregion

       
        public TLSVA06()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVAART();
            DataContext = MV;
            InitializeComponent();
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            base.Window_Loaded(sender, e);
            if (!TBMain.Columns.Contains("WALIEFNR"))
                TBMain.Columns.Add("WALIEFNR");
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            if(dwMain==null)
            { }
            if (dwMaster == null)
            {
                KEY = ""; 
                ART = "";
                PDART = "";
            }
            else
            {  
                KEY = dwMaster["VAAKOLL"].ToString() + dwMaster["VAAART"].ToString(); 
                ART = dwMaster["VAAART"].ToString();
                PDART = dwMaster["VAADART"].ToString() + dwMaster["VAADVAR"].ToString();
                ARTST = dwMaster["VAAARTST"].ToString();
            }
        } 
         
       
        public override void OnEXAUFChg(string vlaue)
        {
            Locate("VAAART", EXAUF.PadRight(29).Substring(0,29));
            base.OnEXAUFChg(vlaue);
        }


        public void OnKOLLChg()
        {
            if (VKOLL.ToString().Length == 0)
            {
                TBMain.Clear();
                return;
            }
            KOLL = VKOLL;
            dwData = TBMain.NewRow();
            dwData["VAAMANDANT"] = app.Mandant;
            dwData["VAAKOLL"] = VKOLL;
            dwData["VAAARTST"] = ARTST;
            SResult = TMV.QVA09(dwData);
            MergeData(SResult, false);
            OnLIEFNRChg(LIEFNR);

        }
        public override void OnLIEFNRChg(string ls)
        {
            
            filter = "WALIEFNR LIKE '"+ls+"%'";
            cvMaster.CustomFilter = filter;
            base.OnLIEFNRChg(ls);
        }
        public override void ManageCommand(object sender, RoutedEventArgs e)
        {
            if (IsManage)
            {
                OnKOLLChg();
            }
            else
            {
                switch (ARTST)
                {
                    case "F":
                        VICC_WFEVENTS.SVWF.FVA06(RUNID);
                        break;
                    case "G":
                        VICC_WFEVENTS.SVWF.FVA06(RUNID);
                        break;
                }
            }
            base.ManageCommand(sender, e);
        }

    }
}
